var currPage = 1;
var dataDetail=null;
var inColorList = null;
var outColorList = null;
var id = null;
;layui.define(function(e) {
    layui.use(['table','layer','form','laydate'], function(){
         var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;
         
         $(function(){
        	 loadCarSource();
         })
        
         //选择车源配置
         form.on('select(carSourceInfo)', function(data){
        	var id = $("#car_source option:selected").attr("data-id");
        	clearCarSource();
        	loadCarSource(id);
        	// 外颜色
        	loadInColor(id);
        	//内颜色
        	loadOutColor(id);
 		});
         
           
         
        //重新渲染表单    
         function renderForm(){
           layui.use('form', function(){
            	var form = layui.form;
            	form.render();
           });
         }
         
       //初始化，选择品牌下拉列表
       function loadCarSource(id){
    	   var conMap={"id":id};
       	$.ajax({
       		url: hostUrl+'/purchaseIntentioOrderHead/getCarSourcesInfo',
               type: "post",
               data: JSON.stringify(conMap),
               dataType: "json",
               async : false,
               contentType: "application/json; charset=utf-8",
               beforeSend:function(){
               },
               success: function(data) {
            	   if(data.status == 1){
            		   var mydata = data.carSources;
            		   if(id){
            			   for ( var i = 0; i <mydata.length; i++){
            				   var name =  mydata[i].brand_name+"/"+mydata[i].series_name+"/"+mydata[i].car_type_name;
            				   var value = mydata[i].id;
            				   $('input[name=brand_name]').val(mydata[i].brand_name);
            				   $('input[name=id]').val(value);
            				   $('input[name=car_name]').val(name);
            				   $('input[name=series_name]').val(mydata[i].series_name);
            				   $('input[name=car_type_name]').val(mydata[i].car_type_name);
            				   $('input[name=start_amount]').val(mydata[i].start_amount);//起订数量
            				   inColorList = null;
            				   outColorList = null;
            				   inColorList = mydata[i].in_color.split("、"); // 内颜色
            				   outColorList = mydata[i].out_color.split("、"); //外颜色
            			   }
            		   }else{
            			   for ( var i = 0; i <mydata.length; i++){
            				   var name =  mydata[i].brand_name+"/"+mydata[i].series_name+"/"+mydata[i].car_type_name;
            				   var value = mydata[i].id;
            				   $("#car_source").append("<option data-id='"+value+"' value='"+name+"'>"+name+"</option>");
            			   }
            		   }
            		   renderForm();
            	   }else{
            		   Layer.showFailMsg("获取车源信息失败,请稍后再试...");
            	   }
               },
       		error: function(data) { 
       			Layer.showFailMsg("获取车源信息失败,请稍后再试...");
       	 	}
           }); 
       }
       // 外颜色
       function loadOutColor(id){
    	   if(outColorList.length){
    		   if(id){
    			   $("#out_color option:not(:first)").remove();
    			   for ( var i = 0; i <outColorList.length; i++){
    				   var outColor = outColorList[i];
    				   $("#out_color option:eq(0)").after("<option value='"+outColor+"'>"+outColor+"</option>");
    			   }
    		   }
    	   }
    	   renderForm();
    	   
       }
       //内颜色
       function loadInColor(id){
    	   if(inColorList.length){
    		   if(id){
    			   $("#in_color option:not(:first)").remove();
    			   for ( var i = 0; i <inColorList.length; i++){
    				   var inColor = inColorList[i];
    				   $("#in_color option:eq(0)").after("<option value='"+inColor+"'>"+inColor+"</option>");
    			   }
    		   }
    	   }
    	   renderForm();
       }
       
     //清除内颜色外颜色
      function clearCarSource(){
       	$('input[name=in_color]').val("");
       	$('input[name=out_color]').val("");
       	$('input[name=car_name]').val("");
       	$('input[name=id').val("");
       	$('input[name=brand_name]').val("");
       	$('input[name=series_name]').val("");
       	$('input[name=car_type_name]').val("");
       }
      
    });
});